﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLLPC.Utilities;
using System.IO;

namespace VisionCore
{
    public class Status_Insp1 : ProcedureHelper_Exc
    {
        string subStatusCode = "";
        string tempStatusCode = "";
        bool condition = false;
        bool start = false;
        int RetryTimes = 0;

        SystemTimeCounter scStatus = new SystemTimeCounter();

        public void Main()
        {

            //bool Existence = Kernel.Exc.DI.X033_1_InspCV_Exist1 == 1 && Kernel.Exc.DI.X035_1_InspCV_Exist3 == 1;
            //bool Empty= Kernel.Exc.DI.X033_1_InspCV_Exist1== 0 && Kernel.Exc.DI.X035_1_InspCV_Exist3 == 0&& Kernel.Exc.DI.X036_1_InspStage_Exist == 0 && Kernel.Exc.DI.X037_1_InspStage_Stop == 0;

            bool CCDReady = Kernel.motion.CCD1_X.IsHomeAlready && Kernel.motion.CCD1_Y.IsHomeAlready && Kernel.motion.CCD1_Z.IsHomeAlready;
           
            condition = Kernel.motion.Insp1_Y.IsHomeAlready && CCDReady;

            start = condition && AutoMode;

            switch (Insp1StatusCode)
            {
                case InspStatus.Init:
                    subStatusCode = "00: 准备";
                    {
                        if (start)
                        {
                            Insp1StatusCode = InspStatus.CheckStatus;

                        }
                    }
                    break;
                case InspStatus.CheckStatus:
                    subStatusCode = "05: 状态判断";
                    {
                        if (ProcedureHelper_Exc.Insp1_Empty) 
                        {
                            Insp1StatusCode = InspStatus.Empty;
                        }
                    }
                    break;
                case InspStatus.Empty:
                    subStatusCode = "10: 检测模组无料";
                    {
                        Insp1StatusCode = InspStatus.LoadPrepare;
                    }
                    break;
                case InspStatus.LoadPrepare:
                    subStatusCode = "11: 准备接料";
                    {
                        Insp1StatusCode = InspStatus.Load;
                    }
                    break;
                #region 开始接料
                case InspStatus.Load:
                    subStatusCode = "20: 开始接料";
                    {
                        if (CV1StatusCode == LDCVStatus.Send_Wait_1st)
                        {
                            Insp1StatusCode = InspStatus.PinCylinder_ToUp;
                        }                      
                    }
                    break;
                case InspStatus.PinCylinder_ToUp:
                    subStatusCode = "21: 阻挡气缸上升";
                    if (ProcedureHelper_Exc.Insp1_Cyl_Up)
                    {

                        Insp1StatusCode = InspStatus.Receiving;
                        scStatus.Start();
                    }
                    break;
                case InspStatus.Receiving:
                    subStatusCode = "22: 接料中";
                    {
                        if ((DryRunMode && scStatus.Duration > 5000) || (!DryRunMode && Insp1_Hard_Existence))
                        {
                            Insp1StatusCode = InspStatus.RevCompleted;
                        }
                    }
                    break;
                case InspStatus.RevCompleted:
                    subStatusCode = "23: 接料结束";
                    if (Insp1CV_Stop)
                    {
                        if (!Insp1_Pass)
                        {
                            Insp1StatusCode = InspStatus.WaitToInsp;
                        }
                        else
                        {
                            Insp1StatusCode = InspStatus.Unload;
                        }
                    }
                    break;
                case InspStatus.WaitToInsp:
                    subStatusCode = "24: 等待检测";
                    {
                        Insp1StatusCode = InspStatus.InspTrigger;
                    }
                    break;
                #endregion
                #region 等待检测
                case InspStatus.InspTrigger:
                    subStatusCode = "30: 进入检测流程";
                    {
                        Insp1StatusCode = InspStatus.InspST_RM_ZToWork;
                    }
                    break;
                case InspStatus.InspST_RM_ZToWork:
                    subStatusCode = "31: 力控上升";
                    {
                        if (Insp1_RM_Z_Work)
                        {
                            Insp1StatusCode = InspStatus.ST_VacOn;
                        }
                    }
                    break;
                case InspStatus.ST_VacOn:
                    subStatusCode = "32: 载台吸真空";
                    {
                        if (Insp1_ToVacOnn)
                        {
                            Insp1StatusCode = InspStatus.Scanning;
                        }
                        if (DryRunMode)
                        {
                            scStatus.Start();
                        }
                    }
                    break;
                case InspStatus.Scanning:
                    subStatusCode = "33: 开始检测";
                    {
                        if (DryRunMode && scStatus.Duration>5000)
                        {
                            Insp1StatusCode = InspStatus.InspCompleted;
                        }
                        if (!DryRunMode)
                        {
                            Insp1StatusCode = InspStatus.InspCompleted;
                        }
                    }
                    break;
                case InspStatus.InspCompleted:
                    subStatusCode = "34: 检测结束";
                    {
                        Insp1StatusCode = InspStatus.ST_VacOff;
                    }
                    break;
                case InspStatus.ST_VacOff:
                    subStatusCode = "35: 载台破真空";
                    {
                        if (!Insp1_VacOnn)
                        { 
                            Insp1StatusCode = InspStatus.InspST_RM_ZToWait;
                        }
                    }
                    break;
                case InspStatus.InspST_RM_ZToWait:
                    subStatusCode = "36: 力控下降";
                    {
                        if (Insp1_RM_Z_Wait)
                        {
                            Insp1StatusCode = InspStatus.Unload;
                        }                       
                    }
                    break;
                #endregion
                #region 开始出料
                case InspStatus.Unload:
                    subStatusCode = "50:等待出料";
                    {
                        Insp1StatusCode = InspStatus.PinCylinder_ToDown;
                    }
                    break;
                case InspStatus.PinCylinder_ToDown:
                    subStatusCode = "51: 阻挡气缸下降";
                    if (Insp1_Cyl_Dn)                    
                    {
                        Insp1StatusCode = InspStatus.SendReady;
                    }
                    break;
                case InspStatus.SendReady:
                    subStatusCode = "52: 准备出料";
                    {
                        if((Kernel.Exc.DI.X068_DnStr1_RevReady == 1 && Kernel.Exc.DI.X069_DnStr1_Receiving == 1)|| true)
                        {
                            Insp1StatusCode = InspStatus.SendOut;
                            scStatus.Start();
                        }                        
                    }
                    break;
                case InspStatus.SendOut:
                    subStatusCode = "53: 出料中";
                    {
                        if ((!DryRunMode && Kernel.Exc.DI.X068_DnStr1_RevReady==0 && Kernel.Exc.DI.X069_DnStr1_Receiving == 0 && Insp1_Hard_Nothing)|| (DryRunMode && scStatus.Duration > 3000))
                        {
                            Insp1StatusCode = InspStatus.SendCompeted;
                        }
                    }
                    break;
                case InspStatus.SendCompeted:
                    subStatusCode = "54: 出料结束";
                    {
                        if (Insp1CV_Stop)
                        {
                            Insp1StatusCode = InspStatus.End;
                        }
                    }
                    break;
                #endregion
                case InspStatus.Error:
                    subStatusCode = "90: 流程报错";
                    {
                        Insp1StatusCode = InspStatus.Empty;
                    }
                    break;
                case InspStatus.End:
                    subStatusCode = "100: 流程结束";
                    {
                        Insp1StatusCode = InspStatus.LoadPrepare;
                    }
                    break;
                default:
                    break;
            }
            LogStatusCode(subStatusCode);
        }

            private void LogStatusCode(string msg)
        {
            if (msg != tempStatusCode)
            {
                tempStatusCode = msg;
                LogMsg("检测模组1-" + tempStatusCode);
            }
        }

    }
}
